package org.i0o.appliancemanager.ui.helper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;

public class Clock {

	private JLabel	lblDatetime;
	private String	dateFormat;
	private SimpleDateFormat	sdf;

	public Clock(JLabel compont) {
		this.lblDatetime = compont;
	}
	public String getDateFormat() {
		
		return dateFormat;
	}


	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}


	
	public void initDateFormat() {
		if(dateFormat == null ||dateFormat == "") {
			dateFormat = "yyyy-MM-dd HH:mm:ss";
		}
		this.sdf =  new SimpleDateFormat(dateFormat);
	}
	

	/**
	 * 开始计时
	 */
	public void start() {
		initDateFormat();
		Timer timer = new Timer();
		timer.schedule(new RefreshTask(), 1000, 1000);
	}

	private class RefreshTask extends TimerTask {

		@Override
		public void run() {
			Date time = new Date(System.currentTimeMillis());
			String dateStr = sdf.format(time);
			
			lblDatetime.setText(dateStr);

		}

	}

}
